<html>
<head><meta charset="utf-8"><title>get name of crate · t-compiler/help · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/index.html">t-compiler/help</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/get.20name.20of.20crate.html">get name of crate</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="231407396"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/get%20name%20of%20crate/near/231407396" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Noah Lev <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/get.20name.20of.20crate.html#231407396">(Mar 23 2021 at 00:53)</a>:</h4>
<p>How do I get the name of a <code>hir::Crate</code>? The only thing I've been able to think of so far is to use <a href="https://doc.rust-lang.org/nightly/nightly-rustc/rustdoc/clean/types/struct.Span.html#method.cnum"><code>rustdoc::Span::cnum()</code></a> (the code I'm writing is in rustdoc) and then using <code>TyCtxt::crate_name()</code>, but using <code>krate.item.span.clean().cnum()</code> seems really hacky. I find it surprising that <code>hir::Crate</code>s don't know their CrateNum nor their name.</p>



<a name="231407620"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/get%20name%20of%20crate/near/231407620" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/get.20name.20of.20crate.html#231407620">(Mar 23 2021 at 00:56)</a>:</h4>
<p>try <a href="https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/ty/context/struct.GlobalCtxt.html#structfield.crate_name"><code>tcx.crate_name</code></a></p>



<a name="231407664"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/get%20name%20of%20crate/near/231407664" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/get.20name.20of.20crate.html#231407664">(Mar 23 2021 at 00:57)</a>:</h4>
<p>if it's a <code>hir::Crate</code> that means it must be the crate currently being compiled</p>



<a name="231407735"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/get%20name%20of%20crate/near/231407735" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Noah Lev <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/get.20name.20of.20crate.html#231407735">(Mar 23 2021 at 00:58)</a>:</h4>
<p>I know I need <code>crate_name</code>, but how do I get the CrateNum to give that function?</p>



<a name="231407741"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/get%20name%20of%20crate/near/231407741" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/get.20name.20of.20crate.html#231407741">(Mar 23 2021 at 00:58)</a>:</h4>
<p>no, not the function, the field</p>



<a name="231408879"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/get%20name%20of%20crate/near/231408879" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Noah Lev <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/get.20name.20of.20crate.html#231408879">(Mar 23 2021 at 01:12)</a>:</h4>
<p>Oh! <span aria-label="face palm" class="emoji emoji-1f926" role="img" title="face palm">:face_palm:</span></p>



<a name="231409019"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/get%20name%20of%20crate/near/231409019" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Noah Lev <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/get.20name.20of.20crate.html#231409019">(Mar 23 2021 at 01:14)</a>:</h4>
<p>Thanks!</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>